package com.example.finger.adpter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.finger.R;
import com.example.finger.bean.LockLog;

import java.util.List;

/**
 * Created by LYX on 2018/11/29.
 */

public class LogAdapter extends BaseAdapter {
    List<LockLog> list;
    Context context;

    public LogAdapter(List<LockLog> list, Context context){
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adpter_log, null);

            viewHolder = new ViewHolder();
            viewHolder.textViewLockNum = convertView.findViewById(R.id.tv_lock_num);
            viewHolder.textViewNum = convertView.findViewById(R.id.tv_num);
            viewHolder.textViewStatu = convertView.findViewById(R.id.tv_statu);
            viewHolder.textViewDate = convertView.findViewById(R.id.tv_date);
            viewHolder.textViewOp = convertView.findViewById(R.id.tv_op);
            viewHolder.layout = convertView.findViewById(R.id.layout);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        LockLog lockLog = list.get(position);
        viewHolder.textViewNum.setText((position +1)+"");
        if(TextUtils.equals("0", lockLog.getState())){
            viewHolder.textViewStatu.setText("关锁");
            viewHolder.layout.setBackgroundColor(context.getResources().getColor(R.color.sea));
        }else {
            viewHolder.textViewStatu.setText("开锁");
            viewHolder.layout.setBackgroundColor(context.getResources().getColor(R.color.white));
        }

        viewHolder.textViewDate.setText(lockLog.getTime().substring(5));
        viewHolder.textViewLockNum.setText(lockLog.getLocknum());
        viewHolder.textViewOp.setText(lockLog.getOperator() + " " + lockLog.getOperator2());

        return convertView;
    }

    class ViewHolder {
        public TextView textViewLockNum;
        public TextView textViewNum;
        public TextView textViewStatu;
        public TextView textViewDate;
        public TextView textViewOp;
        public RelativeLayout layout;
    }
}
